package Timer;

import javax.sound.midi.Soundbank;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

class Test22{
    int a=0;
    public void add(){
        a++;
    }

}
class MyTask implements java.util.Comparator<MyTask> {
    private Runnable runnable;
    private  long time;

    public MyTask(Runnable runnable, long time) {
        this.runnable = runnable;
        this.time = time;
    }

    public long getTime(){
        return this.time;
    }

    public void run(){
        this.runnable.run();
    }

    @Override
    public int compare(MyTask o1, MyTask o2) {
        return (int) (o1.getTime()-o2.getTime());
    }
}

class Comparator implements java.util.Comparator<MyTask>{



    @Override
    public int compare(MyTask o1, MyTask o2) {
        return (int) (o1.getTime()-o2.getTime());
    }
}

public class MyTimer {

    private Thread  thread = null;

    private PriorityBlockingQueue<MyTask> blockingQueue = new PriorityBlockingQueue<>();

    public MyTimer(){

        thread = new Thread(()->{
                while(true) {
                    synchronized (this) {
                        try {
                            MyTask task = blockingQueue.take();
                            if (task.getTime() < System.currentTimeMillis()) {
                                blockingQueue.put(task);
                                this.wait(System.currentTimeMillis()- task.getTime());
                            }
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
        });
    }
    public static void main6(String[] args) throws InterruptedException {

        AtomicInteger atomicInteger = new AtomicInteger(0);
        atomicInteger.incrementAndGet();
        System.out.println(atomicInteger);

        Thread thread1  = new Thread(()->{
            for(int i = 0;i<5000;i++){
                atomicInteger.incrementAndGet();
            }
        });

        Thread thread2 = new Thread(()->{
            for(int i = 0;i<5000;i++){
                atomicInteger.incrementAndGet();
            }
        });
        thread1.start();
        thread2.start();
        thread2.join();
        thread1.join();
        System.out.println(atomicInteger);

       int tmp = 0;
       Test22 test22 = new Test22();
        Thread thread3 = new Thread(()->{
            for(int i = 0;i<5000;i++){
              test22.add();
            }
        });

        Thread thread4 = new Thread(()->{
            for(int i = 0;i<5000;i++){
                test22.add();
            }
        });
        thread3.start();
        thread4.start();

    }


    public static void main(String[] args) {
        int a = 10;
       Test22 test22 = new Test22();
        Thread thread1 = new Thread(()->{
            while(true){
                System.out.println("a的值为："+test22.a);
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        Thread thread2 = new Thread(()->{
            try {
                Thread.sleep(10000);
                test22.a=20;
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        thread1.start();

        thread2.start();

    }
    public static void main1(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(8, ()->{
            System.out.println("全部就绪，开始游戏");
        });

        for(int i = 0;i<8;i++){
           Thread t =  new Thread(()->{
                try {
                    Thread.sleep(1000);
                    System.out.println("线程："+Thread.currentThread().getName()+"已经就绪");
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } catch (BrokenBarrierException e) {
                    throw new RuntimeException(e);
                }
            });
           t.start();
            System.out.println("水友："+Thread.currentThread().getName()+"进入了游戏");

        }

    }



}
